﻿<!-- Views/Account/ChangePassword.cshtml -->
@model WebApplication1.Models.ChangePasswordModel

@{
    ViewBag.Title = "修改密码";
    Layout = "~/Views/Shared/_Layout.cshtml";
}

<div class="container">
    <div class="row">
        <div class="col-md-6 offset-md-3">
            <div class="card mt-5">
                <div class="card-header bg-primary text-white">
                    <h4 class="mb-0">
                        <i class="glyphicon glyphicon-lock"></i> 修改密码
                    </h4>
                </div>
                <div class="card-body">
                    <!-- 显示成功消息 -->
                    @if (TempData["SuccessMessage"] != null)
                    {
                        <div class="alert alert-success">
                            <i class="glyphicon glyphicon-ok"></i>
                            @TempData["SuccessMessage"]
                        </div>
                    }

                    <!-- 显示错误消息 -->
                    @if (ViewBag.ErrorMessage != null)
                    {
                        <div class="alert alert-danger">
                            <i class="glyphicon glyphicon-exclamation-sign"></i>
                            @ViewBag.ErrorMessage
                        </div>
                    }

                    <!-- 显示模型验证错误 -->
                    @if (!ViewData.ModelState.IsValid)
                    {
                        <div class="alert alert-danger">
                            @Html.ValidationSummary(true, "", new { @class = "text-danger" })
                        </div>
                    }

                    @using (Html.BeginForm("ChangePassword", "Account", FormMethod.Post))
                    {
                        @Html.AntiForgeryToken()

                        <div class="form-group">
                            @Html.LabelFor(model => model.OldPassword, "当前密码", new { @class = "font-weight-bold" })
                            @Html.PasswordFor(model => model.OldPassword, new { @class = "form-control", placeholder = "请输入当前密码" })
                            @Html.ValidationMessageFor(model => model.OldPassword, "", new { @class = "text-danger" })
                        </div>

                        <div class="form-group">
                            @Html.LabelFor(model => model.NewPassword, "新密码", new { @class = "font-weight-bold" })
                            @Html.PasswordFor(model => model.NewPassword, new { @class = "form-control", placeholder = "请输入新密码（至少6位字符）" })
                            @Html.ValidationMessageFor(model => model.NewPassword, "", new { @class = "text-danger" })
                            <small class="form-text text-muted">密码长度至少6位字符</small>
                        </div>

                        <div class="form-group">
                            @Html.LabelFor(model => model.ConfirmPassword, "确认新密码", new { @class = "font-weight-bold" })
                            @Html.PasswordFor(model => model.ConfirmPassword, new { @class = "form-control", placeholder = "请再次输入新密码" })
                            @Html.ValidationMessageFor(model => model.ConfirmPassword, "", new { @class = "text-danger" })
                        </div>
                        <br />
                        <div class="form-group">
                            <button type="submit" class="btn btn-primary btn-block">
                                <i class="glyphicon glyphicon-ok"></i> 确认修改
                            </button>
                            <a href="@Url.Action("Index", Session["Role"].ToString() == "Admin" ? "Admin" : "Employee")"
                               class="btn btn-secondary btn-block ">
                                <i class="glyphicon glyphicon-arrow-left"></i> 返回首页
                            </a>
                        </div>
                    }
                </div>
            </div>
        </div>
    </div>
</div>
<script src="~/Scripts/jquery-3.6.0.min.js"></script>
<script src="~/Scripts/bootstrap.min.js"></script>

<script>
    $(document).ready(function () {
        // 表单提交处理
        $('form').submit(function () {
            var oldPassword = $('#OldPassword').val();
            var newPassword = $('#NewPassword').val();
            var confirmPassword = $('#ConfirmPassword').val();

            // 基本验证
            if (!oldPassword) {
                alert('请输入当前密码');
                return false;
            }

            if (!newPassword) {
                alert('请输入新密码');
                return false;
            }

            if (newPassword.length < 6) {
                alert('新密码长度至少6位字符');
                return false;
            }

            if (newPassword !== confirmPassword) {
                alert('新密码和确认密码不一致');
                return false;
            }

            // 显示提交状态
            $('button[type=submit]').prop('disabled', true)
                .html('<i class="glyphicon glyphicon-hourglass"></i> 正在修改...');
        });

        // 自动隐藏成功消息
        if ($('.alert-success').length > 0) {
            setTimeout(function () {
                $('.alert-success').fadeOut('slow');
            }, 3000);
        }
    });
</script>